//
//  DrawerViewController.m
//  PCNews
//
//  Created by PengchengWang on 16/3/1.
//  Copyright © 2016年 pengchengWang. All rights reserved.
//

#import "DrawerViewController.h"
#import "AppDelegate.h"

@interface DrawerViewController ()<UIGestureRecognizerDelegate>

@end

@implementation DrawerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //如果没有手势
    if (!self.tap) {
        self.tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
        [self.tap setEnabled:NO];
        self.tap.delegate = self;
        [self.view addGestureRecognizer:self.tap];
    }
    [self.mbPreHUD hide:YES];
}

//打开左抽屉
-(void)openMenu{
    [self showleftViewController:nil];
    [self.tap setEnabled:YES];
    self.rootVC.view.userInteractionEnabled = NO;
}

//打开右抽屉
-(void)openRightVC{
    [self showRightViewController:nil];
    [self.tap setEnabled:YES];
    self.rootVC.view.userInteractionEnabled = NO;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"openRightV" object:nil];
}

//手势关闭抽屉
-(void)tapAction{
    [self showRootViewController:nil];
    [self.tap setEnabled:NO];
    self.rootVC.view.userInteractionEnabled = YES;
}

//设置中间视图控制器
-(void)setRootViewController:(UIViewController*)viewcontroller{
    //如果传进来的viewController是空的直接返回
    if (!viewcontroller) {
        return;
    }
    //设置中间视图控制器
    [self.rootVC.view removeFromSuperview];
    self.rootVC = viewcontroller;
    [self addChildViewController:viewcontroller];
    [self.view addSubview:viewcontroller.view];
    //如果手势交互开着,证明抽屉是打开的
    if (self.tap.enabled) {
        viewcontroller.view.frame = CGRectMake(200, 0, 414, 736);
    }
    //关闭抽屉
    [self showRootViewController:nil];
    //设置导航栏
    [self setLeftNaButton];
    [self setRightNaButton];
}

//展示中间视图的方法(关闭抽屉)
-(void)showRootViewController:(UIViewController*)rootViewController{
    [UIView animateWithDuration:0.4 animations:^{
        self.rootVC.view.frame = CGRectMake(0, 0, 414, 736);
    }];
    //手势关掉
    [self.tap setEnabled:NO];
    //rootView交互打开
    self.rootVC.view.userInteractionEnabled = YES;
}

//打开左抽屉
-(void)showleftViewController:(UIViewController*)leftViewController{
    //手势打开
    [self.tap setEnabled:YES];
    //rootView禁止使用
    self.rootVC.view.userInteractionEnabled = NO;
    /**
     *  当右抽屉存在时切换左右抽屉在视图中的层次,不存在右抽屉时直接添加左抽屉
     */
    if (![self.view.subviews containsObject:self.rightVC.view]){
        [self.view insertSubview:self.leftVC.view atIndex:0];
    }else {
        [self.view insertSubview:self.leftVC.view aboveSubview:self.rightVC.view];
    }
    //动画移动根视图
    [UIView animateWithDuration:0.4 animations:^{
        self.rootVC.view.frame = CGRectMake(200, 0, 414, 736);
    }];
}

//打开右抽屉
-(void)showRightViewController:(UIViewController*)rightViewController{
    //手势打开
    [self.tap setEnabled:YES];
    //rootView禁止使用
    self.rootVC.view.userInteractionEnabled = NO;
    /**
     *  只添加一次视图(不写也行)
     */
    if (![self.view.subviews containsObject:self.leftVC.view]){
        [self.view insertSubview:self.rightVC.view atIndex:0];
    }else {
        [self.view insertSubview:self.rightVC.view aboveSubview:self.leftVC.view];
    }
    //动画移动根视图
    [UIView animateWithDuration:0.4 animations:^{
        self.rootVC.view.frame = CGRectMake(-200, 0, 414, 736);
    }];
}

//设置左上角按钮
-(void)setLeftNaButton{
    UINavigationController *na = (UINavigationController*)self.rootVC;
    UIViewController *vc = na.viewControllers[0];
    vc.navigationItem.leftBarButtonItem = ({
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"main_menu_left"] style:(UIBarButtonItemStyleDone) target:self action:@selector(openMenu)];
        item;
    });
}

//设置右上角按钮
-(void)setRightNaButton{
    UINavigationController *na = (UINavigationController*)self.rootVC;
    UIViewController *vc = na.viewControllers[0];
    vc.navigationItem.rightBarButtonItem = ({
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"main_menu_right"] style:(UIBarButtonItemStyleDone) target:self action:@selector(openRightVC)];
        item;
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//手势代理方法(实现抽屉打开时右边视图不能处理事件)
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if (gestureRecognizer == self.tap) {
        return CGRectContainsPoint(self.view.frame, [gestureRecognizer locationInView:_rootVC.view]);
    }
    return YES;
}

+(void)setRootViewController:(Class)rootClass{
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    DrawerViewController *drawer = app.draw;
    UIViewController *rootVC = [[rootClass alloc]init];
    UINavigationController *rootNaVC = [[UINavigationController alloc]initWithRootViewController:rootVC];
    [rootNaVC.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar_bg_7"] forBarMetrics:UIBarMetricsDefault];
    [drawer setRootViewController:rootNaVC];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
